// pages/layerFieldPage/layerFieldPage.js
import {
  request
} from '../../request/index.js';
import {
  rightJudge
} from '../../lib/right.js';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 当前图层名称
    layerName: '',
    // 当前图层ID
    layerId: '',
    // 当前图层的字段
    layerField: '',
    // 添加的图层字段
    addedFieldName: '',
    // 标记是编辑还是添加
    flag: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      layerName: options.layerName,
      layerId: options.layerId
    })
    // 发请求查询字段
    request({
      url: '/layer/' + this.data.layerId
    }).then(result => {
      this.setData({
        layerField: result.data.data.fieldList
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (this.data.layerField.length != 0) {
      app.globalData.layerGlobal.map(v => {
        if (v._id == this.data.layerId) {
          v.fieldList = this.data.layerField
        }
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 添加字段
  handleAddField: function () {
    // 判断权限
    if (app.globalData.teamUserRight != 2) {
      wx.showModal({
        title: '提示',
        content: '您还没有编辑权限！请联系管理员'
      })
    } else {
      this.setData({
        showModal: true
      })
      this.setData({
        flag: 'add'
      })
      this.setData({
        editField: ''
      })
    }
  },
  close_mask: function () {
    this.setData({
      showModal: false
    })
  },
  // 输入字段
  inputLayerField: function (e) {
    this.setData({
      addedFieldName: e.detail.value
    })
  },
  // 确定
  modifyLayerField: async function (e) {
    // 构建数据
    if (this.data.flag == 'add') {
      wx.showLoading({
        title: '加载中',
        mask: true
      });
      let sendedData = {}
      sendedData['modifyLayerId'] = this.data.layerId
      sendedData['modifyFieldName'] = 'fieldList'
      sendedData['modifyFieldNameCon'] = this.data.addedFieldName
      let res = await request({
        url: '/layer/addLayerField',
        method: 'POST',
        data: sendedData
      }, false)
      // 重新赋值
      this.setData({
        layerField: res.data.data.fieldList
      })
      // 关闭窗口
      this.setData({
        showModal: false
      })
      // 给点添加字段
      let sendedMarkerData = {}
      sendedMarkerData['modifyLayerId'] = this.data.layerId
      sendedMarkerData['modifyFieldName'] = this.data.addedFieldName
      sendedMarkerData['modifyFieldNameCon'] = ''
      let result = await request({
        url: '/marker/addMarkerField',
        method: 'POST',
        data: sendedMarkerData
      }, false)
      wx.hideLoading()
    } else if (this.data.flag == 'edit') {
      let sendEditData = {}
      sendEditData['newField'] = this.data.addedFieldName
      sendEditData['oldField'] = this.data.editField
      sendEditData['layerId'] = this.data.layerId
      wx.showLoading({
        title: '加载中',
        mask: true
      });
      await request({
        url: '/layer/modifyLayerField?newField=' + sendEditData['newField'] + '&oldField=' + sendEditData['oldField'] + '&layerId=' + sendEditData['layerId'],
        method: 'PUT'
      }, false)
      // 重新获取数据
      let result = await request({
        url: '/layer/' + this.data.layerId
      }, false)
      this.setData({
        layerField: result.data.data.fieldList
      })
      // 关闭窗口
      this.setData({
        showModal: false
      })
      wx.hideLoading()
    }

  },
  // 删除字段
  deleteLayerField: async function (e) {
    let userChoice = await wx.showModal({
      title: '提示',
      content: '确认要删除该字段?请谨慎操作'
    })
    if (userChoice.confirm) {
      // 构建数据
      let sendedData = {}
      sendedData['modifyLayerId'] = this.data.layerId
      sendedData['modifyFieldName'] = 'fieldList'
      sendedData['modifyFieldNameCon'] = e.currentTarget.dataset.fieldname
      let res = await request({
        url: '/layer/deleteLayerField',
        method: 'POST',
        data: sendedData
      })
      // 重新赋值
      this.setData({
        layerField: res.data.data.fieldList
      })
      // 删除点数据
      let sendedMarkerData = {}
      sendedMarkerData['modifyLayerId'] = this.data.layerId
      sendedMarkerData['modifyFieldName'] = e.currentTarget.dataset.fieldname
      sendedMarkerData['modifyFieldNameCon'] = ''
      let res2 = await request({
        url: '/marker/deleteMarkerField',
        method: 'POST',
        data: sendedMarkerData
      })
    } else {

    }
  },
  editLayerField(e) {
    this.setData({
      flag: 'edit'
    })
    this.setData({
      showModal: true
    })
    this.setData({
      editField: e.currentTarget.dataset.fieldname
    })
  },
  close_mask: function () {
    this.setData({
      showModal: false
    })
  },
})